#!/usr/bin/env node

const fs = require('fs');
const path = require('path');

// 颜色输出
const colors = {
  reset: '\x1b[0m',
  bright: '\x1b[1m',
  red: '\x1b[31m',
  green: '\x1b[32m',
  yellow: '\x1b[33m',
  blue: '\x1b[34m',
  cyan: '\x1b[36m'
};

function log(message, color = 'reset') {
  console.log(`${colors[color]}${message}${colors.reset}`);
}

function checkFileExists(filePath) {
  return fs.existsSync(filePath);
}

function main() {
  log('🎨 M3U8 Downloader 图标转换工具', 'bright');
  log('================================', 'cyan');
  
  const buildResourcesDir = 'build-resources';
  const svgIconPath = path.join(buildResourcesDir, 'icon.svg');
  
  // 检查SVG图标是否存在
  if (!checkFileExists(svgIconPath)) {
    log('❌ 未找到SVG图标文件', 'red');
    log(`请确保 ${svgIconPath} 文件存在`, 'yellow');
    return;
  }
  
  log('✅ 找到SVG图标文件', 'green');
  log('');
  
  // 检查各种格式的图标
  const iconFormats = [
    { name: 'Windows ICO', path: path.join(buildResourcesDir, 'icon.ico'), required: true },
    { name: 'macOS ICNS', path: path.join(buildResourcesDir, 'icon.icns'), required: true },
    { name: 'Linux PNG', path: path.join(buildResourcesDir, 'icon.png'), required: true }
  ];
  
  let missingIcons = [];
  
  iconFormats.forEach(format => {
    if (checkFileExists(format.path)) {
      log(`✅ ${format.name}: 已存在`, 'green');
    } else {
      log(`❌ ${format.name}: 缺失`, 'red');
      if (format.required) {
        missingIcons.push(format);
      }
    }
  });
  
  if (missingIcons.length === 0) {
    log('');
    log('🎉 所有图标文件都已准备就绪！', 'green');
    return;
  }
  
  log('');
  log('📋 需要转换的图标格式:', 'yellow');
  missingIcons.forEach(format => {
    log(`  - ${format.name}: ${format.path}`, 'cyan');
  });
  
  log('');
  log('🔧 转换方法:', 'yellow');
  log('');
  
  // Windows ICO
  if (!checkFileExists(path.join(buildResourcesDir, 'icon.ico'))) {
    log('Windows ICO 转换:', 'cyan');
    log('1. 在线转换: https://convertico.com/', 'blue');
    log('2. 使用 ImageMagick:', 'blue');
    log('   convert icon.svg -resize 256x256 icon.ico', 'green');
    log('3. 使用 Inkscape:', 'blue');
    log('   inkscape icon.svg --export-filename=icon.ico --export-width=256 --export-height=256', 'green');
    log('');
  }
  
  // macOS ICNS
  if (!checkFileExists(path.join(buildResourcesDir, 'icon.icns'))) {
    log('macOS ICNS 转换:', 'cyan');
    log('1. 在线转换: https://cloudconvert.com/svg-to-icns', 'blue');
    log('2. 使用 iconutil (macOS):', 'blue');
    log('   # 首先创建iconset目录', 'green');
    log('   mkdir icon.iconset', 'green');
    log('   # 生成不同尺寸的PNG', 'green');
    log('   sips -z 16 16 icon.svg --out icon.iconset/icon_16x16.png', 'green');
    log('   sips -z 32 32 icon.svg --out icon.iconset/icon_16x16@2x.png', 'green');
    log('   sips -z 32 32 icon.svg --out icon.iconset/icon_32x32.png', 'green');
    log('   sips -z 64 64 icon.svg --out icon.iconset/icon_32x32@2x.png', 'green');
    log('   sips -z 128 128 icon.svg --out icon.iconset/icon_128x128.png', 'green');
    log('   sips -z 256 256 icon.svg --out icon.iconset/icon_128x128@2x.png', 'green');
    log('   sips -z 256 256 icon.svg --out icon.iconset/icon_256x256.png', 'green');
    log('   sips -z 512 512 icon.svg --out icon.iconset/icon_256x256@2x.png', 'green');
    log('   sips -z 512 512 icon.svg --out icon.iconset/icon_512x512.png', 'green');
    log('   sips -z 1024 1024 icon.svg --out icon.iconset/icon_512x512@2x.png', 'green');
    log('   # 生成ICNS文件', 'green');
    log('   iconutil -c icns icon.iconset', 'green');
    log('');
  }
  
  // Linux PNG
  if (!checkFileExists(path.join(buildResourcesDir, 'icon.png'))) {
    log('Linux PNG 转换:', 'cyan');
    log('1. 在线转换: https://cloudconvert.com/svg-to-png', 'blue');
    log('2. 使用 ImageMagick:', 'blue');
    log('   convert icon.svg -resize 256x256 icon.png', 'green');
    log('3. 使用 Inkscape:', 'blue');
    log('   inkscape icon.svg --export-filename=icon.png --export-width=256 --export-height=256', 'green');
    log('');
  }
  
  log('💡 提示:', 'yellow');
  log('- 建议图标尺寸: 256x256 像素', 'blue');
  log('- 确保图标文件放在 build-resources/ 目录中', 'blue');
  log('- 转换完成后重新运行此脚本检查', 'blue');
  log('');
  log('📖 更多信息请查看 BUILD.md 文档', 'cyan');
}

// 运行主函数
main();
